/*global module:false*/
module.exports = function(grunt) {
	readOptionalJSON = function(filepath) {
		var data = {};
		try {
			data = grunt.file.readJSON(filepath);
		} catch (e) {
		}
		return data;
	};
	var config = {
		filepath : "package.json",
		prefix   : "src/main/resources"
	};
	
	processRelativePath = function(filepath , prefix, flag ) {
		var data = readOptionalJSON(filepath),
		pathArray = [],
		newPathArray = [];
		var csscontent = grunt.file.read( prefix + flag );
		var regexp = new RegExp("\\@import url\\(\"\/"+data.moduleName+"(.*?)\"\\);", "g");
		console.log( "============ css file =============" );
		while((result=regexp.exec(csscontent)) != null) {
			pathArray.push(result[1]);
			console.log( prefix + result[1] );
		}
		for ( var index=0 ; index < pathArray.length ; index++) {
			var newpath = pathArray[index].substring(0, pathArray[index]
					.lastIndexOf("/") + 1);
			var fileName = pathArray[index].substring(pathArray[index]
					.lastIndexOf("/") + 1, pathArray[index].lenth);
			var newFilePath = newpath + "tmp_" + fileName;
			newPathArray[index] = prefix+newFilePath;
			var content = grunt.file.read(prefix+pathArray[index], "utf8").toString();
			var newContent = content.replace(/url\(["']?(.*?)["']?\)/g, "url(" + "/"+ data.moduleName + newpath+ "$1)");
			grunt.file.write(prefix+newFilePath, newContent);
		}
		return newPathArray;
	};
  
  getResource = function( filepath ,prefix,  flag )
  {
	  var data = readOptionalJSON(filepath);
	  var jscontent =  grunt.file.read( prefix + flag  );
	  var regexp = new RegExp("document\\.write\\(\"<script src='\/"+data.moduleName+"(.*?)' type='text\/javascript'><\/script>\"\\);", "g");
	  var sourceArr = ["<banner:meta.banner>"];
	  console.log( "============ js file =============" );
	  while((result=regexp.exec(jscontent)) != null) {
		  sourceArr.push(prefix + result[1]);
		  console.log( prefix + result[1] );
	  }
	  
	  return sourceArr;
  };
	
	// Project configuration.
	grunt.initConfig({
				pkg : '<json:package.json>',
				meta : {
					banner : '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - '
							+ '<%= grunt.template.today("yyyy-mm-dd") %>\n'
							+ '<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>'
							+ '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;'
							+ ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
				},
				concat : {
					task_dist : {
						src : getResource("package.json" , config.prefix , "/resources/js/monitor-task.min.js"),
						dest : '<%= pkg.prefix %>/resources/js/<%= pkg.name %>-task.min.js'
					},
					job_dist : {
						src : getResource("package.json" , config.prefix , "/resources/js/monitor-job.min.js"),
						dest : '<%= pkg.prefix %>/resources/js/<%= pkg.name %>-job.min.js'
					},
					agent_dist : {
						src : getResource("package.json" , config.prefix , "/resources/js/monitor-agent.min.js"),
						dest : '<%= pkg.prefix %>/resources/js/<%= pkg.name %>-agent.min.js'
					},
					server_dist : {
						src : getResource("package.json" , config.prefix , "/resources/js/monitor-server.min.js"),
						dest : '<%= pkg.prefix %>/resources/js/<%= pkg.name %>-server.min.js'
					},
					alarm_dist : {
						src : getResource("package.json" , config.prefix , "/resources/js/monitor-alarm.min.js"),
						dest : '<%= pkg.prefix %>/resources/js/<%= pkg.name %>-alarm.min.js'
					},
					taskstatistics_dist : {
						src : getResource("package.json" , config.prefix , "/resources/js/monitor-taskstatistics.min.js"),
						dest : '<%= pkg.prefix %>/resources/js/<%= pkg.name %>-taskstatistics.min.js'
					},
					
					jobstatistics_dist : {
						src : getResource("package.json" , config.prefix , "/resources/js/monitor-jobstatistics.min.js"),
						dest : '<%= pkg.prefix %>/resources/js/<%= pkg.name %>-jobstatistics.min.js'
					},
					
					css_task_dist : {
						src : processRelativePath("package.json" , config.prefix , "/resources/css/monitor-task.min.css" ),
						dest : '<%= pkg.prefix %>/resources/css/<%= pkg.name %>-task.min.css'
					},
					css_job_dist : {
						src : processRelativePath("package.json" , config.prefix ,"/resources/css/monitor-job.min.css"),
						dest : '<%= pkg.prefix %>/resources/css/<%= pkg.name %>-job.min.css'
					}
					,
					css_agent_dist : {
						src : processRelativePath("package.json", config.prefix ,"/resources/css/monitor-agent.min.css"),
						dest : '<%= pkg.prefix %>/resources/css/<%= pkg.name %>-agent.min.css'
					}
					,
					css_server_dist : {
						src : processRelativePath("package.json" ,config.prefix , "/resources/css/monitor-server.min.css"),
						dest : '<%= pkg.prefix %>/resources/css/<%= pkg.name %>-server.min.css'
					},
					css_alarm_dist : {
						src : processRelativePath("package.json" ,config.prefix , "/resources/css/monitor-alarm.min.css"),
						dest : '<%= pkg.prefix %>/resources/css/<%= pkg.name %>-alarm.min.css'
					}
					,
					css_taskstatistics_dist : {
						src : processRelativePath("package.json" , config.prefix ,"/resources/css/monitor-taskstatistics.min.css"),
						dest : '<%= pkg.prefix %>/resources/css/<%= pkg.name %>-taskstatistics.min.css'
					}
					,
					css_jobstatistics_dist : {
						src : processRelativePath("package.json" ,config.prefix , "/resources/css/monitor-jobstatistics.min.css"),
						dest : '<%= pkg.prefix %>/resources/css/<%= pkg.name %>-jobstatistics.min.css'
					}
				},
				min : {
					task_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.task_dist.dest>' ],
						dest : '<config:concat.task_dist.dest>'
					},
					job_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.job_dist.dest>' ],
						dest : '<config:concat.job_dist.dest>'
					},
					agent_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.agent_dist.dest>' ],
						dest : '<config:concat.agent_dist.dest>'
					},
					server_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.server_dist.dest>' ],
						dest : '<config:concat.server_dist.dest>'
					},
					alarm_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.alarm_dist.dest>' ],
						dest : '<config:concat.alarm_dist.dest>'
					},
					taskstatistics_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.taskstatistics_dist.dest>' ],
						dest : '<config:concat.taskstatistics_dist.dest>'
					},
					jobstatistics_dist : {
						src : [ '<banner:meta.banner>',
								'<config:concat.jobstatistics_dist.dest>' ],
						dest : '<config:concat.jobstatistics_dist.dest>'
					}
				},
				cssmin : {
					css_task_dist : {
						src : '<config:concat.css_task_dist.dest>',
						dest: '<config:concat.css_task_dist.dest>'
					},
					css_job_dist : {
						src : '<config:concat.css_job_dist.dest>',
						dest: '<config:concat.css_job_dist.dest>'
					},
					css_agent_dist : {
						src : '<config:concat.css_agent_dist.dest>',
						dest: '<config:concat.css_agent_dist.dest>'
					},
					css_server_dist : {
						src : '<config:concat.css_server_dist.dest>',
						dest: '<config:concat.css_server_dist.dest>'
					},
					css_alarm_dist : {
						src : '<config:concat.css_alarm_dist.dest>',
						dest: '<config:concat.css_alarm_dist.dest>'
					},
					css_taskstatistics_dist : {
						src : '<config:concat.css_taskstatistics_dist.dest>',
						dest: '<config:concat.css_taskstatistics_dist.dest>'
					},
					css_jobstatistics_dist : {
						src : '<config:concat.css_jobstatistics_dist.dest>',
						dest: '<config:concat.css_jobstatistics_dist.dest>'
					}
					
				},
				uglify : {}
			});
	grunt.loadNpmTasks('grunt-css');
	grunt.registerTask('production', 'concat min cssmin');
};
